﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TemplateAddIn
{
    public delegate void ListEvent(Variable var);

    public class VariableList<T> : Dictionary<string, T> where T : Variable
    {
        public event ListEvent ItemAdded;
        public event ListEvent ItemRemoved;
        public event VariableEvent ItemChanged;

        public void Add(T t)
        {
            try
            {
                base.Add(t.Name, t);

                t.ContentChanged += new VariableEvent(t_ContentChanged);

                if (ItemAdded != null)
                    ItemAdded(t);
            }
            catch (Exception ex)
            {
                throw new Exception("This document already contains a variable with that name");
            }
        }

        void t_ContentChanged(Variable sender)
        {
            if (ItemChanged != null)
                ItemChanged(sender);
        }

        public void Remove(T t)
        {
            base.Remove(t.Name);

            if (ItemRemoved != null)
                ItemRemoved(t);
        }
    }
}
